﻿/* 
	Setups the selected SubTool as an instanced Subtool using NanoMesh
*/

/////////////////////////////////////////
// User Changeable Options ///////////////
/////////////////////////////////////////

[VarDef,gKeepSubtoolSize,0] // When enabled the NanoMesh will be created at the same size as the original Subtool (Default 0)
[VarDef,gKeepSubtoolPosition,0] // When enabled the NanoMesh will be created with the same location as the original Subtool (Default 0)
[VarDef,CheckCameraFacing,0] // When enabled the Macro will check to make sure the model is positioned correctly before creating the NanoMesh. Enabling this will make sure the NanoMesh is always aligned correctly. (Default 0)

/////////////////////////////////////////
/////////////////////////////////////////

[RoutineDef,CreateInstanceSubtool,

	// Store Original Tool
	[VarSet,OrigTool,""]
	[VarSet,OrigTool,[IGet,"Tool:ItemInfo"]]

	// Store Current Brush
	[VarSet,BrushID,[IGet,"Brush:Item Info"]]

	// Store Camera Position
	[TransformGet,xPosCam,yPosCam,zPosCam,xScaleCam,yScaleCam,zScaleCam,xAngleCam,yAngleCam,zAngleCam]

	// Get Subtool Size and Position
	[VarSet,SubtoolXYZ,[IGet,"Tool:Geometry:XYZ Size"]]
	[VarSet,SubtoolXPos,[IGet,"Tool:Geometry:X Position"]]
	[VarSet,SubtoolYPos,[IGet,"Tool:Geometry:Y Position"]]
	[VarSet,SubtoolZPos,[IGet,"Tool:Geometry:Z Position"]]

	// Create NanoMesh Brush
	[IKeyPress,'1',[IPress,"Brush:Create:Create InsertMesh"]]
	[IPress,"Brush:Create:Create NanoMesh Brush"]

	// Save out NanoMesh Brush as a file to name it
	[FileNameSetNext,[StrMerge,"ZBRUSH_\ZStartup\BrushPresets\","NanoMeshInstance.dat"]]
	[IPress,"Brush:Save As"]

	// Select the Plane3D object
	[IPress,"Tool:Plane3D"]

	// Change Initialize Settings
	[ISet,"Tool:Initialize:HDivide",2]
	[ISet,"Tool:Initialize:VDivide",2]

	// Make PolyMesh3D
	[IPress,"Tool:Make PolyMesh3D"]

	// Make sure Plane3D object is facing front
	[TransformGet,xPos,yPos,zPos,xScale,yScale,zScale,xRot,yRot,zRot]
	[VarSet,xRot,-0]
	[VarSet,yRot,0]
	[VarSet,zRot,0]
	[TransformSet,xPos,yPos,zPos,xScale,yScale,zScale,xRot,yRot,zRot]

	[IPress,Transform:Fit]
	[TransformSet,[IGet,"document:width"]/2,[IGet,"document:height"]/2,]
	[IClick,1004,[IGet,"document:width"]/2,[IGet,"document:height"]/2,1,1]

	[ISet,Tool:NanoMesh:m.Width,1]
	[ISet,Tool:NanoMesh:m.Length,1]
	[ISet,Tool:NanoMesh:m.Height,1]
	[ISet,Tool:NanoMesh:m.XRotation,0]
	[ISet,Tool:NanoMesh:m.YRotation,0]
	[ISet,Tool:NanoMesh:m.ZRotation,-90]
	[ISet,Tool:NanoMesh:m.Size,1]
	[IPress,Tool:NanoMesh:m.Fit]

	// Change NanoMesh Settings
	[IUnPress,"Tool:NanoMesh:m.ShowPlacement"]
	[IPress,Transform:Fit]

	// Reset the Gizmo3D to center of the Mesh
	[IPress,"Tool:Masking:Go To Unmasked Center"]

	// Copy the Subtool
	[IPress,"Tool:SubTool:Copy"]

	// Delete Tool
	[IKeyPress,'2',[IPress,"Tool:SubTool:Del All"]]

	// Return to original tool
	[ISet,"Tool:ItemInfo",OrigTool]

	// Change Visibility on the Subtool
	[VarSet,activST,[SubToolGetActiveIndex]]
	[SubToolSetStatus,activST,[SubToolGetStatus,activST]-0x1]

	[IPress,"Tool:SubTool:Paste"]

	// Change the NanoMesh properties
	[IPress,"Tool:NanoMesh:m.Edit Mesh"]
	[If,gKeepSubtoolSize,
		[ISet,"Tool:Geometry:XYZ Size",SubtoolXYZ]
	,]

	[If,gKeepSubtoolPosition,
		[ISet,"Tool:Geometry:X Position",SubtoolXPos]
		[ISet,"Tool:Geometry:Y Position",SubtoolYPos]
		[ISet,"Tool:Geometry:Z Position",SubtoolZPos]
	,]
	[IUnPress,"Tool:NanoMesh:m.Edit Mesh"]

	//[IPress,Transform:Fit]
	[TransformSet,xPosCam,yPosCam,zPosCam,xScaleCam,yScaleCam,zScaleCam,xAngleCam,yAngleCam,zAngleCam]

	// Change back to Orig Brush
	[ISet,"Brush:Item Info",BrushID]
]

[IButton,???,"Setups the selected SubTool as an instanced Subtool using NanoMesh (Edit Macro for Options.)",
	[IShowActions,0]
	[IConfig,4.8]

	[If,CheckCameraFacing,
		// Check Mesh Camera Facing
		[TransformGet, xPos, yPos, zPos, xScale, yScale, zScale, xRot, yRot, zRot]
		//Check to make sure the model is positioned correctly before attempting to generate the NanoMesh Instance
		[If,((xRot=-0) && (((abs(yRot)=90) && (abs(zRot)=180)) || ((yRot=0) && (zRot=0)) || ((yRot=180) && (abs(zRot)=180)))),
		,
			[Note,"Error: Model is not positioned facing the front, back, or side on the Canvas. Please reposition and run again."]
			[Exit]
		]
	]

	[IFreeze,
		[RoutineCall,CreateInstanceSubtool]
	]
	[Note,"Position the NanoMesh Poly to the ideal location (You can use CTRL + Drag to duplicate the poly.)\n\nActivate 'Edit Mesh' in the NanoMesh area to modify the shape."]
,,1,,,]